<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 test
 */
class User_Controller extends controller_user0{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','系统管理');
		$this->assign('menuid1','1');  
	 	$this->assign('pagetopimg','user.jpg');
		
		
		
		$this->assign('mainumenuid','1');
		$this->assign('childumenuid','1');
	}
	##################################
	/* 会员首页 */
	################################## 
	function index__(){ 
		$db = factory::getDBO();
		$m_member = factory::module('member');
		$this->assign('rs',$m_member->getuserinfo());  
    
		$rs_sends = array(
			'num1'=>$db->loadresult('select count(a.id) from t_send a left join t_sendpi b on a.sendpi_id=b.id where a.user_id="'.$m_member->getuserid().'" and b.status in (0,1) and b.deleted=0 '),
			'num2'=>$db->loadresult('select count(a.id) from t_send a left join t_sendpi b on a.sendpi_id=b.id where a.user_id="'.$m_member->getuserid().'" and b.status=2 and b.deleted=0 ')
		);
		$this->assign('rs_sends',$rs_sends); 
		
		
		
		$this->assign('mainumenuid','1');
		$this->assign('childumenuid','1');
	} 
	
	
	##################################
	/* 修改资料 */
	##################################
	public function info__(){
		$m_member = factory::module('member');
		$this->assign('rs',$m_member->getuserinfo());  
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) )); 
 
			$db = factory::getDBO(); 
			$db->begin();
			unset($form['password']);
			$m_member = factory::module('member');
			$form['id']=$m_member->getuserid();
		
			//主表
		  if(!$db->update('user',$form)){
				exit('操作失败！"}');
			}
			# 重新获取用户信息到session
			
			$_SESSION['sys_user'] = $db->loadassoc('select * from `t_user` where id="'.$form['id'].'" ');
			
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}	
		 
		$this->assign('mainumenuid','1');
		$this->assign('childumenuid','3');
		
	}
	##################################
	/* 修改密码 */
	##################################
	function pwd__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$_POST = format::addslash_deep($_POST);
			$db = factory::getDBO();
			$m_member = factory::module('member');
			$sql = 'select * from `#__user` where id="'.$m_member->getuserid().'" and password="'.$_POST['old_p'].'" ';
			if($db->loadassoc($sql)){
				$arr = array(
					"id" => $m_member->getuserid(),
					"password" => $_POST['new_p']
				);
				if($db->update('user',$arr)){
					exit('{success:1,msg:"修改密码成功!"}');
				}else{
					exit('系统繁忙，请稍后重试！');
				}
			}else{
				exit('操作失败，原密码输入有误！');
			} 
		}	
		
		$this->assign('mainumenuid','1');
		$this->assign('childumenuid','3');
	}
	
	
	
	/* 配送历史查询 */
	public function send__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$m_member = factory::module('member');
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			
  	  #查询条件
			$sql_where = ' where b.deleted=0 and b.status="2" and a.user_id="'.$m_member->getuserid().'" '.func::speedsearch($_POST['keywords'],array( 'b.code' 	,'b.senddate'  ));  
		
			if($_POST['b_date']!=''){
				$sql_where .= ' and b.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and b.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];		
		  
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_send` a left join t_sendpi b on a.sendpi_id=b.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*,
					b.status,
					b.senddate,
					b.code
				from `t_send` a left join t_sendpi b on a.sendpi_id=b.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as $k=>&$v){
					$v['veges'] = factory::module('packet')->decode($v['packet']);
					$v['veges_string'] = factory::module('packet')->decode($v['packet'],'string');
					$v['veges_html'] = factory::module('packet')->decode($v['packet'],'html');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
		
		$this->assign('mainumenuid','2');
		$this->assign('childumenuid','2');
		
	}
	
	
	/* 蔬菜溯源 */
	public function vegesource__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$db = factory::getDBO();
			$m_member = factory::module('member');
			$_POST=format::addslash_deep($_POST);
			$code = $_POST['code'];
			
			$rs_send = $db->loadassoc('
				select 
					a.*,
					b.status,
					b.senddate,
					b.code,
					b.onsource
				from `t_send` a 
				left join t_sendpi b on a.sendpi_id=b.id 
				where b.deleted=0 and b.status="2" and a.user_id="'.$m_member->getuserid().'" and b.code="'.$code.'" and b.onsource="1"
			');
			$rs_source = factory::module('source')->getSourceByUserSendpi($m_member->getuserid(),$rs_send['sendpi_id']);
			
      if($rs_source){
				exit('{success:1,results:'.json_encode($rs_source).'}');
			}else{
				exit('{success:0,results:[]}');
			}
			
			exit();  
		}
		
		
		
		$pro_fields = factory::module('source')->getField('1');
		$make_fields = factory::module('source')->getField('2');
		$send_fields = factory::module('source')->getField('3');
		$this->assign('pro_fields',$pro_fields); 
		$this->assign('make_fields',$make_fields); 
		$this->assign('send_fields',$send_fields);
		$this->assign('mainumenuid','2');
		$this->assign('childumenuid','3');
		
	}
	
	
	/* 配送计划 */
	public function now__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$m_member = factory::module('member');
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'b.senddate';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'ASC';
  	  #查询条件
			$sql_where = ' where b.deleted=0 and b.status in (0,1)  and a.user_id="'.$m_member->getuserid().'" ';
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_send` a left join t_sendpi b on a.sendpi_id=b.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*,
					b.status,
					b.senddate,
					b.code,
					c.sltnum
				from `t_send` a 
				left join t_sendpi b on a.sendpi_id=b.id  
				left join t_sendfen c on a.sendfen_id=c.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as $k=>&$v){
					# 当前会员配送的实际选择菜品信息
					$rs_send_vege = $db->loadassoclist('select a.*,b.code,b.name from `t_send_vege` a left join t_vege b on a.vege_id=b.id  where a.send_id="'.$v['id'].'" ');
					$v['rs_send_vege'] = $rs_send_vege; 
					
					
					$v['veges'] = factory::module('packet')->decode($v['packet']);
					$v['veges_string'] = factory::module('packet')->decode($v['packet'],'string');
					$v['veges_html'] = factory::module('packet')->decode($v['packet'],'html');
					 
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
		
		$this->assign('mainumenuid','2');
		$this->assign('childumenuid','1');
		
	}
	
	

	# 修改配送批次中的可选配送内容，固定项目是不能修改 ，只能修改可选项目，如果该配送批次没有可选项，那么修改操作就完全不能操作任何数据！
	public function now_modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$vegeids = format::addslash_json(json_decode( stripslashes($_POST['vegeids']) ));
			$_POST=format::addslash_deep($_POST);
		  $send_id = $_POST['send_id'];
			$db = factory::getDBO();
			
			
			

			$rs_send = $db->loadassoc('select * from t_send where id="'.$send_id.'"');
			$rs_sendpi = $db->loadassoc('select * from t_sendpi where id="'.$rs_send['sendpi_id'].'" and deleted=0 ');
			$rs_sendfen = $db->loadassoc('select * from t_sendfen where id="'.$rs_send['sendfen_id'].'" ');
			if(!$rs_send || !$rs_sendpi || !$rs_sendfen){
				exit('操作失败，没有找到处理记录！');
			}
			if($rs_sendpi['status']=='1'){
				exit('操作失败，当前所属配送批次正在生产中！');
			}
			if($rs_sendpi['status']=='2'){
				exit('操作失败，当前所属配送批次已经完成！');
			}			
			
			if(!factory::module('vege')->updatesltvege($send_id,$vegeids)){
				exit(factory::module('vege')->getError());
			}
			exit('{success:1,msg:"操作成功！"}');
		}	
		$_GET=format::addslash_deep($_GET);
		$send_id = intval($_GET['id']);
		$db = factory::getDBO();
		
	  # 配送信息
		$rs_send = $db->loadassoc('select a.*,b.username,b.name from `t_send` a left join t_user b on a.user_id=b.id where  a.id="'.$send_id.'" and a.user_id="'.factory::module('member')->getuserid().'" ');
		if(!$rs_send){
			exit('没有查到任何记录！');
		}
		$this->assign('rs_send',$rs_send);
		
	  # 配送批次信息
		$rs_sendpi = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendpi` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.deleted=0 and a.id="'.$rs_send['sendpi_id'].'" ');
		if(!$rs_sendpi){
			exit('没有查到任何记录！');
		}
		if($rs_sendpi['status']=='1'){
			//exit('操作失败，当前所属配送批次正在生产中！');
			//	url::jump('/user/now_modify_lock?msg=当前配送正在生产中，如需修改配送菜品，请拨打我们的客服电话 400-0900-917!');
		}
		if($rs_sendpi['status']=='2'){
			exit('操作失败，当前所属配送批次已经完成！');
		}
		$this->assign('rs_sendpi',$rs_sendpi);
		

	  # 配送分支信息
		$rs_sendfen = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendfen` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.id="'.$rs_send['sendfen_id'].'" ');
		if(!$rs_sendfen){
			exit('没有查到任何记录！');
		}
		$this->assign('rs_sendfen',$rs_sendfen);
		
		
		# 当前会员配送的实际选择菜品信息
		$rs_send_vege = $db->loadassoclist('select a.*,b.code,b.name,b.maxnum from `t_send_vege` a left join t_vege b on a.vege_id=b.id  where a.send_id="'.$rs_send['id'].'" ');
		$this->assign('rs_send_vege',$rs_send_vege);  
		
		
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
		
		
		
	}
	
	##################################
	/*  体质测试首页 */
	##################################
	function body__(){
		$db = factory::getDBO(); 
		$rs = $db->loadassoc('select * from t_body where deleted=0 and isuser=1 and user_id="'.factory::module('member')->getuserid().'" '); 
		$this->assign('rs',$rs);  
		
		$db = factory::getDBO(); 
		$rs_qin = $db->loadassoclist('select * from t_body where deleted=0 and isuser=0 and user_id="'.factory::module('member')->getuserid().'" order by id desc '); 
		$this->assign('rs_qin',$rs_qin);  
		$this->assign('mainumenuid','3');
		$this->assign('childumenuid','1');
	}
	
	
	
	
	
	
	
	
	
	
	##################################
	/*  亲友体质测试保存 */
	##################################
	/* 添加家人体质测试 - 基本信息 */
	function addfriend__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$db = factory::getDBO(); 
			$db->begin();
			$form=array(
				"user_id"=>factory::module('member')->getuserid(),
				"name"=>$form['name'],
				"sex"=>$form['sex'],
				"isuser"=>"0",
				"main_body"=>'',
				"pian_body"=>'',
				"created"=>'now()'
			);	
			//主表
			$body_id = $db->insert('body',$form);
		  if(!$body_id){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1,body_id:"'.$body_id.'"}');
		}	
	} 
	/* 修改家人基本信息 */
	function modifyfriend__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$db = factory::getDBO(); 
			$db->begin();
			$form=array(
				"id"=>$form['id'],
				"name"=>$form['name'],
				"sex"=>$form['sex']
			);
		  if(!$db->update('body',$form)){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1}');
		}	
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select * from `t_body` a  where deleted=0 and user_id="'.factory::module('member')->getuserid().'" and id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs',$rs);
	} 
	/*  会员体质测试保存 */ 
	function body_friend_modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$main_body = format::addslash_json(json_decode( stripslashes($_POST['main_body']) ));
			$pian_body = format::addslash_json(json_decode( stripslashes($_POST['pian_body']) ));
			$_POST=format::addslash_deep($_POST);
			$body_id = intval($_POST['body_id']);
			$db = factory::getDBO(); 
			$db->begin(); 
			$form=array(
				"id"=>$body_id,
				"user_id"=>factory::module('member')->getuserid(),
				"isuser"=>"0",
				"main_body"=>implode(',',$main_body),
				"pian_body"=>implode(',',$pian_body)
			);	
			//主表
		  if(!$db->update('body',$form)){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1}');
		}
	}
	/* 会员体质测试删除 */
	public function body_friend_delete__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$body_id = intval($_POST['body_id']);
			$db = factory::getDBO(); 
			$db->begin();
			# 删除以前的
		  if(!$db->query('update t_body set deleted=1 where isuser=0 and user_id="'.factory::module('member')->getuserid().'"  and id="'.$body_id.'" ')){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	
	
	
	
	
	##################################
	/*  会员体质测试保存 */
	##################################
	
	
	/*  会员体质测试保存 */ 
	function body_member_modify__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$main_body = format::addslash_json(json_decode( stripslashes($_POST['main_body']) ));
			$pian_body = format::addslash_json(json_decode( stripslashes($_POST['pian_body']) ));
			$m_member = factory::module('member');
			$db = factory::getDBO(); 
			$db->begin();
			# 删除以前的
			$db->query('update t_body set deleted=1 where isuser=1 and user_id="'.factory::module('member')->getuserid().'" ');
			$form=array(
				"user_id"=>$m_member->getuserid(),
				"isuser"=>"1",
				"main_body"=>implode(',',$main_body),
				"pian_body"=>implode(',',$pian_body),
				"created"=>'now()'
			);	
			//主表
		  if(!$db->insert('body',$form)){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1}');
		}
	}
	/* 会员体质测试删除 */
	public function body_member_delete__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$db = factory::getDBO(); 
			$db->begin();
			# 删除以前的
		  if(!$db->query('update t_body set deleted=1 where isuser=1 and user_id="'.factory::module('member')->getuserid().'" ')){
				exit('操作失败！"}');
			}
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	
	 
	
	 
}
?>